import mongoose from "mongoose";

//评论
const reviewSchema = mongoose.Schema(
  {
    // 所属用户
    user: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "User",
    },
    // 用户姓名
    name: {
      type: String,
      required: true,
    },
    // 评分
    rating: {
      type: Number,
      required: true,
    },
    //评价信息
    comment: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

//商品
const productSchema = mongoose.Schema(
  {
    // 更改创建的用户
    user: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "User",
    },
    // 商品名
    name: {
      type: String,
      required: true,
    },
    //图片
    image: {
      type: String,
      required: true,
    },
    // 品牌
    brand: {
      type: String,
      required: true,
    },
    // 分类
    category: {
      type: String,
      required: true,
    },
    // 介绍
    description: {
      type: String,
      required: true,
    },
    // 价格
    price: {
      type: Number,
      required: true,
      default: 0,
    },
    //库存
    countInStock: {
      type: Number,
      required: true,
      default: 0,
    },
    // 评价几颗星
    rating: {
      type: Number,
      required: true,
    },
    // 多少条评论
    numReviews: {
      type: Number,
      required: true,
    },
    // 评论信息
    reviews: [reviewSchema],
  },
  {
    timestamps: true,
  }
);

const Product = mongoose.model("Product", productSchema);

export default Product;
